﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YLW.Plat.Jhfw.HL7V3.XSDModels.POOR_IN200902UV_Jysq;
using YLW.Plat.Jhfw.Model.JhfwJysqd;

namespace YLW.Plat.Jhfw.HL7V3.V3Message
{
    public class V3_POOR_IN200902UV_Jysq
    {
        public static string ExamAppInfoUpdate(AddJhfwJysqdIn jsonData, string id_extension, string sender_name, string receiver_name)
        {
            string hl7Xml = string.Empty;

            if (jsonData == null)
            {
                return hl7Xml;
            }

            //根据XML XSD构建HL7 XML消息格式

            //性别
            string sexCode = "";
            string sexName = "";
            switch (jsonData.psex)
            {
                case "B":
                    sexCode = "9";
                    sexName = "未说明的性别";
                    break;
                case "F":
                    sexCode = "2";
                    sexName = "女性";
                    break;
                case "M":
                    sexCode = "1";
                    sexName = "男性";
                    break;
                case "U":
                    sexCode = "0";
                    sexName = "未知的性别";
                    break;
            }

            //创建根对象
            var xmlRoot = new POOR_IN200902UV
            {
                ITSVersion = "XML_1.0",
                id = new id { root = "2.16.156.10011.2.5.1.1", extension = id_extension },
                creationTime = new creationTime { value = DateTime.Now.ToString("yyyyMMddHHmmss") },
                interactionId = new interactionId { root = "2.16.156.10011.2.5.1.2", extension = "POOR_IN200902UV" },
                processingCode = new processingCode { code = "P" },
                processingModeCode = new processingModeCode(),
                acceptAckCode = new acceptAckCode { code = "AL" },
                receiver = new receiver
                {
                    typeCode = "RCV",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[] {
                               new item {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = receiver_name
                                }
                            }
                        }
                    }
                },
                sender = new sender
                {
                    typeCode = "SND",
                    device1 = new device1
                    {
                        classCode = "DEV",
                        determinerCode = "INSTANCE",
                        id = new id
                        {
                            item = new item[] {
                                new item {
                                    root = "2.16.156.10011.2.5.1.3",
                                    extension = sender_name
                                }
                            }
                        }
                    }
                },
                controlActProcess = new controlActProcess
                {
                    classCode = "ACTN",
                    moodCode = "EVN",
                    subject = new subject
                    {
                        typeCode = "SUBJ",
                        observationRequest = new observationRequest
                        {
                            classCode = "OBS",
                            moodCode = "RQO",
                            Items = new object[]
                            {
                                new id
                                {
                                    item= new item[]
                                    {
                                        new item{
                                            root="2.16.156.10011.1.24",
                                            extension=jsonData.sqdh
                                        }
                                    }
                                },
                                new code(),
                                new text{
                                    value = jsonData.sqdmc
                                },
                                new statusCode{ code = "active" },
                                new effectiveTime
                                {
                                    Items = new object[]
                                    {
                                        new low
                                        {
                                            value = jsonData.kssj
                                        },
                                        new high
                                        {
                                            value = jsonData.jssj
                                        }
                                    }
                                },
                                new priorityCode
                                {
                                    code = jsonData.yxjdm,
                                    displayName=new displayName{ value = jsonData.yxjmc }
                                },
                                new specimen
                                {
                                    specimen1=new specimen
                                    {
                                        id=new id
                                        {
                                            root="2.16.156.10011.1.14",
                                            extension=jsonData.bbdm
                                        },
                                        code=new code
                                        {
                                            code1=jsonData.bblb,
                                            displayName=new displayName{ value = "" }
                                        },
                                    }
                                },
                                new author
                                {
                                    typeCode="AUT",
                                    time=new time
                                    {
                                        value=jsonData.kdrq
                                    },
                                    signatureText=new signatureText
                                    {
                                        value=jsonData.kdysxm
                                    },
                                    assignedEntity=new assignedEntity
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.1.4",
                                                    extension=jsonData.kdysdm
                                                }
                                            }
                                        },
                                        assignedPerson=new assignedPerson
                                        {
                                            classCode="PSN",
                                            determinerCode="INSTANCE",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=jsonData.kdysxm }
                                                }
                                            }
                                        },
                                        representedOrganization=new representedOrganization
                                        {
                                            classCode="ORG",
                                            determinerCode="INSTANCE",
                                            id=new id
                                            {
                                                item=new item[]
                                                {
                                                    new item
                                                    {
                                                        root="2.16.156.10011.2.3.2.62",
                                                        extension=jsonData.kdksdm
                                                    }
                                                }
                                            },
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=jsonData.kdksmc }
                                                }
                                            }
                                        }
                                    }
                                },
                                new verifier
                                {
                                    typeCode="VRF",
                                    time=new time
                                    {
                                        value=jsonData.shrq
                                    },
                                    assignedEntity=new assignedEntity
                                    {
                                        classCode="ASSIGNED",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.1.4",
                                                    extension=jsonData.shrdm
                                                }
                                            }
                                        },
                                        assignedPerson=new assignedPerson
                                        {
                                            classCode="PSN",
                                            determinerCode="INSTANCE",
                                            name=new name
                                            {
                                                item=new item
                                                {
                                                    Item=new part{ value=jsonData.shrxm }
                                                }
                                            }
                                        }
                                    }
                                },
                                new component2{
                                    observationRequest=new observationRequest
                                    {
                                        classCode="OBS",
                                        moodCode="RQO",
                                        Items=new object[]
                                        {
                                            new id
                                            {
                                                item=new item[]
                                                {
                                                    new item
                                                    {
                                                        root="2.16.156.10011.1.28",
                                                        extension=jsonData.yzid
                                                    }
                                                }
                                            },
                                            new code{
                                                code1=jsonData.jyxmdm,
                                                displayName=new displayName{ value = jsonData.jyxmmc }
                                            },
                                            new statusCode(),
                                            new methodCode
                                            {
                                                item=new item
                                                {
                                                    code=jsonData.jyff,
                                                    Item=new displayName
                                                    {
                                                        value=""
                                                    }
                                                }
                                            },
                                            new location
                                            {
                                                typeCode="LOC",
                                                time=new time
                                                {
                                                    any=new any{ value=jsonData.zxsj }
                                                },
                                                serviceDeliveryLocation=new serviceDeliveryLocation
                                                {
                                                    classCode="SDLOC",
                                                    serviceProviderOrganization=new serviceProviderOrganization
                                                    {
                                                        classCode="ORG",
                                                        determinerCode="INSTANCE",
                                                        id=new id
                                                        {
                                                            item=new item[]
                                                            {
                                                                new item
                                                                {
                                                                    root="2.16.156.10011.2.3.2.62",
                                                                    extension=jsonData.zxksdm
                                                                }
                                                            }
                                                        },
                                                        name=new name
                                                        {
                                                            item=new item
                                                            {
                                                                Item=new part{ value=jsonData.zxksmc }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new subjectOf6
                                {
                                    contextConductionInd=false,
                                    seperatableInd=new seperatableInd { value=false },
                                    annotation=new annotation
                                    {
                                        text=new text
                                        {
                                            value=jsonData.remark
                                        },
                                        statusCode=new statusCode { code="completed" },
                                        author=new author
                                        {
                                            assignedEntity=new assignedEntity
                                            {
                                                classCode="ASSIGNED"
                                            }
                                        }
                                    }
                                },
                                new componentOf1
                                {
                                    contextConductionInd=false,
                                    typeCode="COMP",
                                    encounter=new encounter
                                    {
                                        classCode="ENC",
                                        moodCode="EVN",
                                        id=new id
                                        {
                                            item=new item[]
                                            {
                                                new item
                                                {
                                                    root="2.16.156.10011.1.11",
                                                    extension=""
                                                },
                                                new item
                                                {
                                                    root="2.16.156.10011.1.12",
                                                    extension=""
                                                }
                                            }
                                        },
                                        code=new code
                                        {
                                            code1=jsonData.stay,
                                            codeSystem="2.16.156.10011.2.3.1.271",
                                            codeSystemName="患者类型代码",
                                            displayName=new displayName
                                            {
                                                value=""
                                            }
                                        },
                                        statusCode=new statusCode (),
                                        subject=new subject
                                        {
                                            typeCode="SBJ",
                                            patient=new patient
                                            {
                                                classCode="PAT",
                                                id=new id
                                                {
                                                    item=new item[]
                                                    {
                                                        new item
                                                        {
                                                            root="2.16.156.10011.2.5.1.4",
                                                            extension=jsonData.mrn
                                                        }
                                                    }
                                                },
                                                patientPerson=new patientPerson
                                                {
                                                    classCode="PSN",
                                                    id=new id
                                                    {
                                                        item=new item[]
                                                        {
                                                            new item
                                                            {
                                                                root="2.16.156.10011.1.3",
                                                                extension=jsonData.pcid
                                                            },
                                                            new item
                                                            {
                                                                root="2.16.156.10011.1.15",
                                                                extension=jsonData.ybkh
                                                            }
                                                        }
                                                    },
                                                    name=new name
                                                    {
                                                        item=new item
                                                        {
                                                            Item=new part{ value=jsonData.pname }
                                                        }
                                                    },
                                                    telecom=new telecom
                                                    {
                                                        item=new item
                                                        {
                                                            value=jsonData.pphone
                                                        }
                                                    },
                                                    administrativeGenderCode=new administrativeGenderCode
                                                    {
                                                        code=sexCode,
                                                        codeSystem="2.16.156.10011.2.3.3.4",
                                                        displayName=new displayName
                                                        {
                                                            value=sexName
                                                        }
                                                    },
                                                    birthTime=new birthTime
                                                    {
                                                        value=jsonData.pbirthday,
                                                        originalText=new originalText
                                                        {
                                                            value=""
                                                        }
                                                    },
                                                    addr=new addr
                                                    {
                                                        item=new item
                                                        {
                                                            use="H",
                                                            Item=new part
                                                            {
                                                                type="AL",
                                                                value=jsonData.paddress
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        location=new location
                                        {
                                            typeCode="LOC",
                                            time=new time(),
                                            serviceDeliveryLocation=new serviceDeliveryLocation
                                            {
                                                classCode="SDLOC",
                                                location=new location
                                                {
                                                    classCode="PLC",
                                                    determinerCode="INSTANCE",
                                                    Items=new object[]
                                                    {
                                                        new id
                                                        {
                                                            item=new item[]
                                                            {
                                                                new item
                                                                {
                                                                    extension=jsonData.bedid
                                                                }
                                                            }
                                                        },
                                                        new name
                                                        {
                                                            item=new item
                                                            {
                                                                use="IDE",
                                                                Item=new part{ value="" }
                                                            }
                                                        },
                                                        new asLocatedEntityPartOf
                                                        {
                                                            classCode="LOCE",
                                                            location=new location
                                                            {
                                                                classCode="PLC",
                                                                determinerCode="INSTANCE",
                                                                Items=new object[]
                                                                {
                                                                    new id{
                                                                        item=new item[]{
                                                                            new item
                                                                            {
                                                                                extension=jsonData.bfh
                                                                            }
                                                                        }
                                                                    },
                                                                    new name
                                                                    {
                                                                        item=new item
                                                                        {
                                                                            use="IDE",
                                                                            Item=new part{ value="" }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                },
                                                serviceProviderOrganization=new serviceProviderOrganization
                                                {
                                                    classCode="ORG",
                                                    determinerCode="INSTANCE",
                                                    id=new id
                                                    {
                                                        item=new item[]
                                                        {
                                                            new item{ extension=jsonData.zyksdm }
                                                        }
                                                    },
                                                    name=new name
                                                    {
                                                        item=new item
                                                        {
                                                            use="IDE",
                                                            Item=new part{ value=jsonData.zyksmc }
                                                        }
                                                    },
                                                    asOrganizationPartOf=new asOrganizationPartOf
                                                    {
                                                        classCode="PART",
                                                        wholeOrganization=new wholeOrganization
                                                        {
                                                            classCode="ORG",
                                                            determinerCode="INSTANCE",
                                                            id=new id
                                                            {
                                                                item=new item[]
                                                                {
                                                                    new item{ extension=jsonData.zybqdm }
                                                                }
                                                            },
                                                            name=new name
                                                            {
                                                                item=new item
                                                                {
                                                                    use="IDE",
                                                                    Item=new part{ value=jsonData.zybqmc }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        },
                                        pertinentInformation1=new pertinentInformation1
                                        {
                                            typeCode="PERT",
                                            observationDx=new observationDx
                                            {
                                                classCode="OBS",
                                                moodCode="EVN",
                                                code=new code
                                                {
                                                    code1=jsonData.zdlbdm,
                                                    codeSystem="2.16.156.10011.2.5.1.10",
                                                    displayName=new displayName{ value=jsonData.zdlbmc }
                                                },
                                                statusCode=new statusCode{ code="active" },
                                                effectiveTime=new effectiveTime
                                                {
                                                    Items=new object[]
                                                    {
                                                        new any
                                                        {
                                                            value=jsonData.zdrq
                                                        },
                                                    }
                                                },
                                                value=new value
                                                {
                                                    code=jsonData.jbdm,
                                                    codeSystem="2.16.156.10011.2.3.3.11",
                                                    displayName=new displayName[]
                                                    {
                                                        new displayName{ value=jsonData.jbmc }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                },
                                new extension
                                {
                                    dotype = new dotype { value = new value { Text = new string[] { jsonData.dotype } } },
                                    sjly = new sjly { value = new value { Text = new string[] { jsonData.sjly } } },
                                    lyxt = new lyxt { value = new value { Text = new string[] { jsonData.lyxt } } },
                                    hoscode = new hoscode { value = new value { Text = new string[] { jsonData.hoscode } } },
                                    sqdh = new sqdh { value = new value { Text = new string[] { jsonData.sqdh } } },
                                    yzid = new yzid { value = new value { Text = new string[] { jsonData.yzid } } },
                                    jzlsh = new jzlsh { value = new value { Text = new string[] { jsonData.jzlsh } } },
                                    mrn = new mrn { value = new value { Text = new string[] { jsonData.mrn } } },
                                    pname = new pname { value = new value { Text = new string[] { jsonData.pname } } },
                                    pcid = new pcid { value = new value { Text = new string[] { jsonData.pcid } } },
                                    pbirthday = new pbirthday { value = new value { Text = new string[] { jsonData.pbirthday } } },
                                    psex = new psex { value = new value { Text = new string[] { jsonData.psex } } },
                                    pphone = new pphone { value = new value { Text = new string[] { jsonData.pphone } } },
                                    page = new page { value = new value { Text = new string[] { jsonData.page.ToString() } } },
                                    pageunit = new pageunit { value = new value { Text = new string[] { jsonData.pageunit.ToString() } } },
                                    paddress = new paddress { value = new value { Text = new string[] { jsonData.paddress } } },
                                    ybkh = new ybkh { value = new value { Text = new string[] { jsonData.ybkh } } },
                                    series = new series { value = new value { Text = new string[] { jsonData.series } } },
                                    stay = new stay { value = new value { Text = new string[] { jsonData.stay } } },
                                    zyksdm = new zyksdm { value = new value { Text = new string[] { jsonData.zyksdm } } },
                                    zyksmc = new zyksmc { value = new value { Text = new string[] { jsonData.zyksmc } } },
                                    zybqdm = new zybqdm { value = new value { Text = new string[] { jsonData.zybqdm } } },
                                    zybqmc = new zybqmc { value = new value { Text = new string[] { jsonData.zybqmc } } },
                                    bedid = new bedid { value = new value { Text = new string[] { jsonData.bedid } } },
                                    bfh = new bfh { value = new value { Text = new string[] { jsonData.bfh } } },
                                    jyxmdm = new jyxmdm { value = new value { Text = new string[] { jsonData.jyxmdm } } },
                                    jyxmmc = new jyxmmc { value = new value { Text = new string[] { jsonData.jyxmmc } } },
                                    bbdm = new bbdm { value = new value { Text = new string[] { jsonData.bbdm } } },
                                    bblb = new bblb { value = new value { Text = new string[] { jsonData.bblb } } },
                                    jyff = new jyff { value = new value { Text = new string[] { jsonData.jyff } } },
                                    yxjdm = new yxjdm { value = new value { Text = new string[] { jsonData.yxjdm } } },
                                    yxjmc = new yxjmc { value = new value { Text = new string[] { jsonData.yxjmc } } },
                                    remark = new remark { value = new value { Text = new string[] { jsonData.remark } } },
                                    kssj = new kssj { value = new value { Text = new string[] { jsonData.kssj } } },
                                    jssj = new jssj { value = new value { Text = new string[] { jsonData.jssj } } },
                                    kdksdm = new kdksdm { value = new value { Text = new string[] { jsonData.kdksdm } } },
                                    kdksmc = new kdksmc { value = new value { Text = new string[] { jsonData.kdksmc } } },
                                    kdysdm = new kdysdm { value = new value { Text = new string[] { jsonData.kdysdm } } },
                                    kdysxm = new kdysxm { value = new value { Text = new string[] { jsonData.kdysxm } } },
                                    kdrq = new kdrq { value = new value { Text = new string[] { jsonData.kdrq } } },
                                    jbdm = new jbdm { value = new value { Text = new string[] { jsonData.jbdm } } },
                                    jbmc = new jbmc { value = new value { Text = new string[] { jsonData.jbmc } } },
                                    zdlbdm = new zdlbdm { value = new value { Text = new string[] { jsonData.zdlbdm } } },
                                    zdlbmc = new zdlbmc { value = new value { Text = new string[] { jsonData.zdlbmc } } },
                                    zdrq = new zdrq { value = new value { Text = new string[] { jsonData.zdrq } } },
                                    shrdm = new shrdm { value = new value { Text = new string[] { jsonData.shrdm } } },
                                    shrxm = new shrxm { value = new value { Text = new string[] { jsonData.shrxm } } },
                                    shrq = new shrq { value = new value { Text = new string[] { jsonData.shrq } } },
                                    zxksdm = new zxksdm { value = new value { Text = new string[] { jsonData.zxksdm } } },
                                    zxksmc = new zxksmc { value = new value { Text = new string[] { jsonData.zxksmc } } },
                                    zxsj = new zxsj { value = new value { Text = new string[] { jsonData.zxsj } } },
                                    sqdmc = new sqdmc { value = new value { Text = new string[] { jsonData.sqdmc } } },
                                    sqdzt = new sqdzt { value = new value { Text = new string[] { jsonData.sqdzt } } },
                                    useflag = new useflag { value = new value { Text = new string[] { jsonData.useflag } } },
                                    in_empid = new in_empid { value = new value { Text = new string[] { jsonData.in_empid } } },
                                    in_date = new in_date { value = new value { Text = new string[] { jsonData.in_date } } },
                                    xg_empid = new xg_empid { value = new value { Text = new string[] { jsonData.xg_empid } } },
                                    xgsj = new xgsj { value = new value { Text = new string[] { jsonData.xgsj } } }
                                }
                            }
                        }
                    }
                }
            };

            hl7Xml = HL7V3Helper.SerializeToXml(xmlRoot);

            return hl7Xml;
        }
    }
}
